/*
 * @(#) $Header$
 *
 * Copyright (C) 2006  Daniel Léonard
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

package ca.forklabs.baselib.util.iterator;

import java.util.Enumeration;
import java.util.Iterator;
import ca.forklabs.baselib.util.iterator.Resources;

import java.util.NoSuchElementException;

/**
 * Class {@code IteratorEnumeration} is a adapter to allow an {@link Iterator}
 * to pass as an {@link Enumeration}.
 *
 * @param   <E>   the type of the elements.
 *
 * @author <a href="mailto:forklabs at gmail.com?subject=ca.forklabs.baselib.util.iterator.IteratorEnumeration">Daniel Léonard</a>
 * @version $Revision$
 */
public class IteratorEnumeration<E> implements Enumeration<E> {

//---------------------------
// Instance variable
//---------------------------

   /** The adapted iterator. */
   private Iterator<E> iterator;


//---------------------------
// Constructor
//---------------------------

   /**
    * Constructs an enumeration over the specified iterator.
    * @param   iterator   the adapted iterator.
    * @exception   NullPointerException   if the parameter is {@code null}.
    */
   public IteratorEnumeration(Iterator<E> iterator) {
      this.setIterator(iterator);
      }


//---------------------------
// Accessor and mutator
//---------------------------

   /**
    * Changes the adapted iterator.
    * @param   iterator   the adapted iterator.
    * @exception   NullPointerException   if the parameter is {@code null}.
    */
    protected void setIterator(Iterator<E> iterator) {
      if (null == iterator) {
         String message = this.getCannotAdaptIteratorErrorMessage();
         throw new NullPointerException(message);
         }
      this.iterator = iterator;
      }

   /**
    * Gets the adapted iterator.
    * @return   the adapted iterator.
    */
   protected Iterator<E> getIterator() {
      return this.iterator;
      }


//---------------------------
// Implemented method from java.util.Enumeration
//---------------------------

   /**
    * Tests if this enumeration contains more elements. The enumeration contains
    * more elements if the underlying {@link Iterator} contains more elements.
    * @return   {@code true} if this enumeration object contains at least one
    *           more element, {@code false} otherwise
    */
   @Override
   public boolean hasMoreElements() {
      Iterator<E> iter = this.getIterator();
      boolean has_more_elements = iter.hasNext();
      return has_more_elements;
      }

   /**
    * Returns the next element of this enumeration if this enumeration object
    * has at least one more element to provide.
    * @return   the next element of this enumeration.
    * @exception   NoSuchElementException   if no more elements exist.
    */
   @Override
   public E nextElement() {
      Iterator<E> iter = this.getIterator();
      E element = iter.next();
      return element;
      }


//---------------------------
// Instance methods
//---------------------------

   /**
    * Gets the error message telling that it is impossible to adapt the
    * iterator.
    * @return   the formatted message.
    */
   protected String getCannotAdaptIteratorErrorMessage() {
      String key = Resources.ITERATOR_CANNOT_ADAPT;
      String message = Resources.getLocalizedString(key);
      return message;
      }

   }
